package com.lowca.news.entity;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "t_article_tag", catalog = "com_lowca_news")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ArticleTag extends IdEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private Article article;
	private String name;
	private Integer sortValue;

	public ArticleTag() {
		super();
	}

	public ArticleTag(Article article, String name, Integer sortValue) {
		super();
		this.article = article;
		this.name = name;
		this.sortValue = sortValue;
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "article_id")
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	@Column(name = "name", length = 30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "sort_value")
	public Integer getSortValue() {
		return sortValue;
	}

	public void setSortValue(Integer sortValue) {
		this.sortValue = sortValue;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (!(other instanceof Article))
			return false;
		final ArticleTag at = (ArticleTag) other;
		if (!getName().equals(at.getName()))
			return false;
		if (getArticle() != null && !getArticle().equals(at.getArticle()))
			return false;
		return true;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getName().hashCode();
		result = prime * result
				+ ((getArticle() == null) ? 0 : getArticle().hashCode());
		return result;
	}

}
